<template>
  <div>
    <el-card>
      <el-button class="roles-btn" type="primary" @click="changedialogVisible(true)">添加用户</el-button>
      <el-table :data="rolesList" border style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scoped">
            <el-row class="expand center" :class="['borbottom', index === 0 ? 'bortop' : '']" v-for="(item, index) in scoped.row.children" :key="item.id">
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="closetag(scoped.row, item.id)">{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二、三级权限 -->
              <el-col :span="19">
                <el-row class="center" :class="[subindex === 0 ? '' : 'bortop']" v-for="(subitem, subindex) in item.children" :key="subitem.id">
                  <!-- 二级权限 -->
                  <el-col :span="6">
                    <el-tag type="success" closable @close="closetag(scoped.row, subitem.id)">{{ subitem.authName }} </el-tag>
                    <i class="el-icon-caret-right"></i
                  ></el-col>
                  <el-col :span="18">
                    <el-tag v-for="subsubitem in subitem.children" :key="subsubitem.id" type="warning" closable @close="closetag(scoped.row, subsubitem.id)">{{ subsubitem.authName }}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- <pre>{{ scoped.row }}</pre> -->
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色姓名"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scoped">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editsuser(scoped.row.id)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deluser(scoped.row.id)">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="dispathroles(scoped.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { edituser, deluser, delroles, dispathroles } from '@/api'
import { mapState, mapMutations } from 'vuex'
export default {
  created() {
    this.getRolesList()
  },
  methods: {
    ...mapMutations(['getRolesList', 'changeeditdialog', 'changedialogVisible', 'changedialog', 'getrightslist', 'getLeafkeys', 'getuserid']),
    // ...mapActions(['getRolesList']),

    async editsuser(id) {
      this.changeeditdialog(true)
      // console.log(id)
      const { data: res } = await edituser(id)
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        // this.$message.success(res.meta.msg)
        // this.edituserForm = res.data
        this.setedituserForm(res.data)
      }
    },
    async dispathroles(role) {
      this.getuserid(role.id)
      const { data: res } = await dispathroles()
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        this.getLeafkey(role)
        // this.getLeafkeys(this.Leafkeys)
        this.getrightslist(res.data)
        this.changedialog(true)
      }
    },
    getLeafkey(node) {
      if (!node.children) {
        return this.getLeafkeys(node)
        // return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafkey(item)
      })
    },
    deluser(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await deluser(id)
          // console.log(res)
          if (res.meta.status === 200) {
            this.getusermenu()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: res.meta.msg
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    closetag(user, rolesid) {
      this.$confirm('此操作将删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await delroles(user.id, rolesid)
          // console.log(res)
          if (res.meta.status === 200) {
            // this.getRolesList()
            // console.log(res.data)
            user.children = res.data
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  computed: {
    ...mapState({
      rolesList: state => state.Roles.rolesList
      // Leafkey: state => state.Roles.Leafkey
      // rolesDialog: state => state.Roles.rolesDialog
    })
  }
}
</script>

<style lang="less" scoped>
.roles-btn {
  margin-bottom: 10px;
}
.el-tag {
  margin: 7px;
}
.expand {
  margin: 0 50px;
}
.bortop {
  border-top: 1px solid #ccc;
}
.borbottom {
  border-bottom: 1px solid #ccc;
}
.center {
  display: flex;
  align-items: center;
}
</style>
